package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.EtlBaseService;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * etlBase控制器
 * Created by hozeData on 2021/05/24.
 */
@RestController
@RequestMapping("/etl/etlBase/")
@Api( tags="ETL基础配置")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EtlBaseController {

    @Autowired
    EtlBaseService etlBaseService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.GET)
    public LayMsg findByPage(Integer page, Integer limit,String filter) {
        EtlBase etlBase = new EtlBase();
        etlBase.setTbTar(filter);
        PaginationVO<EtlBase> pageVO = etlBaseService.findPageByModelLike(etlBase, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<EtlBase> pageVO = etlBaseService.findAllByModel(new EtlBase());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="etlBase", value="实体", paramType="body", required=true, dataType="etlBase")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody EtlBase etlBase, HttpServletRequest request)  {
        int num ;
        if(etlBase.getIdBase()==null) {
            num = etlBaseService.insertSelective(etlBase);
        } else {
            num =  etlBaseService.updateByIdSelective(etlBase);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }
    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idBase", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Long idBase, HttpServletRequest request) throws BaseException {
        EtlBase etlBase = etlBaseService.getById(idBase);
        return new BaseResult(CommonResultConstant.SUCCESS, etlBase);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="etlBase", value="实体", paramType="body", required=true, dataType="EtlBase")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody EtlBase etlBase, HttpServletRequest request)  {
        int num = etlBaseService.updateByIdSelective(etlBase);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idBase", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Long idBase, HttpServletRequest request) throws BaseException {
        int num = etlBaseService.deleteById(idBase);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
